package com.itheim.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheim.mapper.StudentMapper;
import com.itheim.pojo.PageResult;
import com.itheim.pojo.Student;
import com.itheim.pojo.StudentQueryParem;
import com.itheim.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> page(StudentQueryParem studentQueryParem) {
        PageHelper.startPage(studentQueryParem.getPage(), studentQueryParem.getPageSize());
        List<Student> clazzList = studentMapper.page(studentQueryParem);
        Page<Student> p=(Page<Student>)clazzList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void addstudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.addstudent(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void delete(Integer[] ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void violationHandle(Integer id, Integer score) {
        studentMapper.violationHandle(id,score,LocalDateTime.now());
    }
}
